﻿/*
 *Copyright(C) 2020 by  GYYX All rights reserved.
 *Unity版本：2018.4.23f1 
 *作者:程一峰  
 *创建日期: 2021-04-08 
 *模块说明：
 *版本: 1.2
*/


using Sirenix.Utilities;
using Sirenix.Utilities.Editor;
using System;
using UnityEditor;
using UnityEngine;
using Warfare.FSM;

namespace WarfareEditor.FSM
{
    /// <summary>
    /// 简易状态机编辑窗口；
    /// </summary>
    public class FSMEditorWindow : EditorWindow
    {

        #region 初始化

        public static FSMEditorWindow window;

        [MenuItem("Tools/简易状态机")]
        public static void ShowWindow()
        {
            window = GetWindow<FSMEditorWindow>();
            window.titleContent = new GUIContent("简易状态机");
            window.position = GUIHelper.GetEditorWindowRect().AlignCenter(800, 500);
            window.autoRepaintOnSceneChange = true;
            window.Show();
        }
        public static void Closed()
        {
            if (null != window)
            {
                window.Close();
                window = null;
            }
        }

        private void OnEnable()
        {
            infoZoom = new FSMZoom_Info();
            infoZoom.Init(this);

            designZoom = new FSMZoom_Design();
            designZoom.Init(this);
        }

        private void OnDestroy()
        {
            infoZoom.OnDestroy();
            designZoom.OnDestroy();
        }

        #endregion

        #region 面板绘制

        private FSMZoom_Info infoZoom;
        private FSMZoom_Design designZoom;

        protected internal FSMAsset CurrentAsset;
        protected internal FSMNode CurrentNode;

        private void OnGUI()
        {
            if (null == window)
            {
                Closed();
                ShowWindow();
                return;
            }

            EditorGUILayout.BeginHorizontal();
            {
                EditorGUILayout.BeginVertical("box", GUILayout.Width(300), GUILayout.ExpandHeight(true));
                infoZoom.OnGUI();
                EditorGUILayout.EndVertical();

                EditorGUILayout.BeginVertical("box", GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(true));
                designZoom.OnGUI();
                EditorGUILayout.EndVertical();
            }
            EditorGUILayout.EndHorizontal();

            Repaint();
        }

        public void DrawWindow(Action callBack)
        {
            // 开始绘制节点 
            // 注意：必须在  BeginWindows(); 和 EndWindows(); 之间 调用 GUI.Window 才能显示
            BeginWindows();
            callBack?.Invoke();
            EndWindows();
        }

        #endregion

    }
}